import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/views/Index/ClassList.dart';
import 'package:thinking/widget/_.dart';

class History extends StatefulWidget {
  @override
  _History createState() => _History();
}

class _History extends State<History> with  AutomaticKeepAliveClientMixin<History>  {

  @override
  bool get wantKeepAlive => true;


  PageController? _pageController;
  int index = 0;
  bool isSplash = true;
  late String title;
  List<HistorySplashItem> historySplashItem = [];
  bool isGetHistorySplashData  = false;

  void initState() {
    super.initState();
    index = SPreferences().getInt('history_Index')==0?0:SPreferences().getInt('history_Index') -1;
    isSplash = SPreferences().getInt('history_Index')==0?true:false;
    _pageController = PageController(initialPage: index);
    getHistorySplashData();
  }


  Future<void> getHistorySplashData() async {

   var res =  await HistoryService.getHistorySplashData(params: {});
   historySplashItem = res!;
   isGetHistorySplashData = true;
   setState(() {

   });

  }

  void dispose() {
    _pageController!.dispose();
    super.dispose();

  }

  Padding historyPage(int e) {
    return Padding(
      padding: EdgeInsets.all(10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(historySplashItem[e].title!,
              style: TextStyle(
                  fontWeight: FontWeight.w600,
                  color: Colors.white,
                  fontSize: 18)),
          SizedBox(
            height: 5,
          ),
          Text(historySplashItem[e].dateTime!,
              style: TextStyle(
                  fontWeight: FontWeight.w600,
                  color: Colors.white,
                  fontSize: 18)),
          SizedBox(
            height: 30,
          ),
          Text(historySplashItem[e].content!,
              style: TextStyle(
                  fontWeight: FontWeight.w600,
                  color: ColorConfig.WhiteObsBackColor,
                  fontSize: 16)),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return this.isSplash?(this.isGetHistorySplashData?Empty(isShow: this.historySplashItem.length == 0,child: Stack(
      children: [
        CachedNetworkImage(
          imageUrl: historySplashItem[index].image!,
          fit: BoxFit.cover,
          height: Screen.height(Config.context) - 135,
          width: Screen.width(Config.context),
          errorWidget: (context, url, error) =>
          new Icon(Icons.error),
          placeholder: (context, url) => Container(
            height: Screen.height(Config.context) - 135,
            width: Screen.width(Config.context),
            color: ColorConfig.ThemeObsColor,
          ),
        ),
        Container(
          padding: EdgeInsets.all(12.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '选择你的时代',
                style: TextStyle(
                    fontWeight: FontWeight.w600,
                    color: Colors.white,
                    fontSize: 24),
              ),
              Expanded(
                child: PageView(
                  physics: BouncingScrollPhysics(),
                  controller: _pageController,
                  onPageChanged: (e) {
                    setState(() {
                      index = e;
                    });
                  },
                  children: historySplashItem
                      .asMap()
                      .keys
                      .map((e) => historyPage(e))
                      .toList(),
                ),
              ),
              Align(
                alignment: Alignment.center,
                child: Container(
                  height: 45,
                  width: 120,
                  margin: EdgeInsets.only(bottom: 20),
                  child: ElevatedButton(
                    style: ButtonStyle(
                      textStyle: MaterialStateProperty.all(
                          TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
                      backgroundColor:
                      MaterialStateProperty.all(ColorConfig.ThemeColor),
                      shape: MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(12)))),
                      elevation: MaterialStateProperty.all(2),
                      foregroundColor: MaterialStateProperty.all(Colors.white),
                    ),
                    onPressed: () async {
                      await SPreferences().setInt('history_Index', historySplashItem[index].id);
                      await SPreferences().setString('history_Title',historySplashItem[index].title);
                      Tool.debounce((){
                        this.isSplash = false;
                        setState(() {
                        });
                      }, Duration(milliseconds: 280));


                    },
                    child: Text('就是它了!'),
                  ),
                ),
              )
            ],
          ),
        )
      ],
    ),):Loading()):ClassList(classValue: '分历史',historySplashCallBack: (){
      index = SPreferences().getInt('history_Index')==0?0:SPreferences().getInt('history_Index') -1;
      _pageController = PageController(initialPage: index);
      this.isSplash =true;
      setState(() {

      });
    },);
  }
}
